import 'package:dio/dio.dart';

import 'interceptor.dart';

///***********************************************
///文件描述：
///创建人：dgg108698
///创建时间：2020/9/14
///修改人：
///修改时间：
///修改备注：
///***********************************************

class DioInterceptorTran extends Interceptor {
  final DggInterceptor interceptor;

  DioInterceptorTran(this.interceptor);

  @override
  Future onRequest(RequestOptions options) async {
    final dggOptions = await interceptor.onRequest(DggRequestOptions(
      method: options.method,
      path: options.path,
      data: options.data,
      headers: options.headers,
    ));
    return dggOptions != null
        ? options.merge(
            method: dggOptions.method,
            path: dggOptions.path,
            data: dggOptions.data,
            headers: dggOptions.headers,
          )
        : super.onRequest(options);
  }

  @override
  Future onResponse(Response response) async {
    final dggResponse = await interceptor.onResponse(DggResponse());
    return dggResponse != null
        ? Response(
            data: dggResponse.data ?? response.data,
            headers: dggResponse.headers != null
                ? Headers.fromMap(dggResponse.headers)
                : response.headers,
            request: response.request,
            isRedirect: response.isRedirect,
            statusCode: response.statusCode,
            statusMessage: response.statusMessage,
            redirects: response.redirects,
            extra: response.extra,
          )
        : super.onResponse(response);
  }
}
